<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index: 1201">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :disabled="!form.busPageInfo.editable"
      size="mini"
      label-width="160px"
    >
      <el-row :gutter="2">
        <el-col :span="12">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect
              v-model="form.busMainData.sysOrgCode"
              :editable="
                form.busPageInfo.editable &&
                form.busMainData.funProcess === '10'
              "
              :has-priv="true"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="线索名称" prop="busMainData.name">
            <el-input v-model="form.busMainData.name" />
          </el-form-item>
          <el-form-item label="客户等级" prop="busMainData.customLevel">
            <el-select
              v-model="form.busMainData.customLevel"
              filterable
              clearable
              placeholder="客户等级"
            >
              <el-option
                v-for="item in dicts.custom_level"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="线索来源" prop="busMainData.sourceType">
            <el-select
              v-model="form.busMainData.sourceType"
              filterable
              clearable
              placeholder="线索来源"
            >
              <el-option
                v-for="item in dicts.custom_source_type"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户行业" prop="busMainData.customTrade">
            <el-select
              v-model="form.busMainData.customTrade"
              filterable
              clearable
              placeholder="线索来源"
            >
              <el-option
                v-for="item in dicts.custom_trade"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属城市" prop="busMainData.cityId">
            <CityCascaderSelect v-model="cityId" style="width: 100%" />
          </el-form-item>
          <el-form-item label="详细地址" prop="busMainData.address">
            <el-input v-model="form.busMainData.address" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机" prop="busMainData.phone">
            <el-input v-model="form.busMainData.phone" />
          </el-form-item>
          <el-form-item label="电话" prop="busMainData.tel">
            <el-input v-model="form.busMainData.tel" />
          </el-form-item>
          <el-form-item label="邮件 " prop="busMainData.email">
            <el-input v-model="form.busMainData.email" />
          </el-form-item>
          <el-form-item label="下次联系时间" prop="busMainData.nextContactTime">
            <el-date-picker
              v-model="form.busMainData.nextContactTime"
              placeholder="下次联系时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item"
            />
          </el-form-item>
          <el-form-item label="负责人" prop="busMainData.chargerName">
            <el-input :disabled="true" v-model="form.busMainData.chargerName" />
          </el-form-item>
          <el-form-item label="最后跟进时间" prop="busMainData.lastFollowDate">
            <el-date-picker
              v-model="form.busMainData.lastFollowDate"
              placeholder="最后跟进时间"
              type="datetime"
              size="mini"
              disabled
              readonly
              class="filter-item"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button
        v-permission="[
          'SYS_ADMIN',
          'COMMON_ADMIN',
          'CRM_CLUE_ALL',
          'CRM_CLUE_CHANGE',
          'CRM_CLUE_CREATE',
        ]"
        :loading="loading"
        type="primary"
        @click="doSave"
        >暂存</el-button
      >
      <el-button
        v-permission="[
          'SYS_ADMIN',
          'COMMON_ADMIN',
          'CRM_CLUE_ALL',
          'CRM_CLUE_SUBMIT',
        ]"
        :loading="loading"
        type="primary"
        @click="doSubmit"
        >提交</el-button
      >
    </div>
  </CustomDialog>
</template>

<script>
import clue from "@/api/crm/Clue";
import { isvalidPhone } from "@/utils/validate";
import CityCascaderSelect from "@/views/components/CityCascaderSelect";
import OrgsetTreeSelect from "@/views/components/OrgsetTreeSelect";

export default {
  components: { CityCascaderSelect, OrgsetTreeSelect },
  dictTypes: ["custom_level", "custom_source_type", "custom_trade"],
  props: {
    sup_this: {
      type: Object,
      required: true,
    },
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入电话"));
      } else if (!isvalidPhone(value)) {
        callback(new Error("请输入正确的11位手机号码"));
      } else {
        callback();
      }
    };
    return {
      isAdd: false,
      loading: false,
      style: "width: 184px",
      dialogInfo: {
        name: "clueMngFormDialog",
        title: "线索管理",
        fullScreen: true,
        width: 500,
        height: 90,
      },
      form: {
        busMainData: {
          id: "",
          enableFlag: "Y",
          cityId: null,
        },
        busPageInfo: {
          editable: false,
        },
      },
      rules: {
        "busMainData.name": [
          { required: true, message: "请输入线索名称", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "长度在 2 到 100 个字符",
            trigger: "blur",
          },
        ],
        "busMainData.sourceType": [
          { required: true, message: "请选择线索来源", trigger: "blur" },
        ],
        // 'busMainData.phone': [
        //   { required: true, trigger: 'blur', validator: validPhone }
        // ],
        "busMainData.customLevel": [
          { required: true, message: "请选择客户等级", trigger: "blur" },
        ],
        "busMainData.customTrade": [
          { required: true, message: "请选择客户行业", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    cityId: {
      get() {
        if (
          this.form.busMainData.cityId &&
          typeof this.form.busMainData.cityId === "string"
        ) {
          return JSON.parse(this.form.busMainData.cityId);
        }
        return this.form.busMainData.cityId;
      },
      set(val) {
        this.form.busMainData.cityId = val;
      },
    },
  },
  mounted() {},
  methods: {
    cancel() {
      this.resetForm();
    },
    initDataOfBefore() {},
    displayDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.show(name);
      });
    },
    hiddenDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.hide(name);
      });
    },
    addData() {
      this.isAdd = true;
      const data = { paramBean: {} };
      clue
        .toAdd(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    copyData(mainId) {
      this.isAdd = true;
      const data = { paramBean: { id: mainId } };
      clue
        .toCopy(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    viewMainData(id) {
      this.isAdd = false;
      const data = { paramBean: { id: id } };
      clue.viewMainData(data).then((res) => {
        this.displayDialog();
        this.form = res.data;
      });
    },
    doSubmit() {
      this.doSingleSave(true);
    },
    doSave() {
      this.doSingleSave(false);
    },
    doSingleSave(isSubmit) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit;
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else {
            this.doEdit();
          }
        }
      });
    },
    doAdd() {
      clue
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    doEdit() {
      clue
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    resetForm() {
      this.hiddenDialog();
      this.$refs["form"].resetFields();
    },
    changeData(busId) {
      const data = { paramBean: { id: busId } };
      clue
        .toChange(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
